/*	
	pptthh AS3 utils / Flex - ActionScript
	Copyright (c) 2009 Peter Toth <pptthh@gmail.com>
	Your reuse is governed by the Creative Commons Attribution 3.0 License
*/
package org.utils.listener
{
// DEF #####################################################
import flash.events.AsyncErrorEvent;
import flash.events.EventDispatcher;
import flash.events.SecurityErrorEvent;
import flash.events.StatusEvent;
import flash.events.TimerEvent;
import flash.net.LocalConnection;
import flash.utils.Timer;

import org.utils.sender.DefaultConnectionConfig;

[Bindable]
[Event(name="stateInfoEvent", type="org.utils.listener.StateInfoEvent")]
public class ListenerCore extends EventDispatcher //implements IConnectionCommon 
{
// DEF #####################################################
public var signRepeater:uint =		DefaultConnectionConfig.SIGN_REPEATER;
public var connectionID:String =	DefaultConnectionConfig.CONNECTION_ID;
public var allowDomain:String =		DefaultConnectionConfig.ALLOW_DOMAIN;
public var allowDomainHost:String =	DefaultConnectionConfig.ALLOW_DOMAIN_HOST;
public function get clientInterface():Object {return client;}
public function set clientInterface(client:Object):void {this.client = client; connect();}

public function ListenerCore():void {super();}

protected function connect():void
{
	closeConnection();
	localConnection = new LocalConnection();
	localConnection.client = client;
	localConnection.allowDomain(allowDomain, allowDomainHost);
	localConnection.allowInsecureDomain(allowDomain, allowDomainHost);
	localConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
	localConnection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
	localConnection.addEventListener(StatusEvent.STATUS, statusHandler);
	connectingTimerEventHandler();
}
// Private ####################################################
private var	timer:Timer
private	var client:Object;
private var localConnection:LocalConnection;

private function connectingTimerEventHandler(timerEvent:TimerEvent=null):void
{
	if(connecting())
	{
		if(!timer) {return;}
		timer.removeEventListener(TimerEvent.TIMER, connectingTimerEventHandler);
		timer.stop();
		timer = undefined;
	}
	else
	{
		if(timer) {return;}
		timer = new Timer(signRepeater);
		timer.addEventListener(TimerEvent.TIMER, connectingTimerEventHandler);
		timer.start();
	}
}
private function connecting():Boolean
{
	StateInfo = 'Trying to connect...';
	try
	{
		localConnection.connect(connectionID);
		StateInfo = 'Connected!';
		return true;
	}
	catch(argumentError:ArgumentError)
	{
		StateInfo = "Can't connect...the connection name is already being used by another SWF... "
	}
	catch(typeError:TypeError)
	{
		StateInfo =
			'TypeError... it should`nt be ... connection error... '+
			'\nmaybe this line is missing somewhere:"localConnection=new LocalConnection(); // =) "'+
			'\nerrorID: '+typeError.errorID+
			'\n'+typeError.message;
	}
	catch(error:Error)
	{
		StateInfo =
			'Unknown connection error...' + 
			'\nerrorID: '+error.errorID+
			'\n'+error.message;
	}
	return false;
}
private function closeConnection():void
{
	try
	{
		localConnection.close();
		localConnection.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
		localConnection.removeEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
		localConnection.removeEventListener(StatusEvent.STATUS, statusHandler);
		localConnection=undefined;
	}
	catch(error:Error){}
}
// Dispach ####################################################
//	stateInfoEvent
private function set StateInfo(currentState:String):void
{
	dispatchEvent(new StateInfoEvent(StateInfoEvent.STATE_INFO_EVENT,currentState));
} 
// Events #####################################################
protected function securityErrorHandler(securityErrorEvent:SecurityErrorEvent):void{}
protected function asyncErrorHandler(asyncErrorEvent:AsyncErrorEvent):void{}
protected function statusHandler(statusEvent:StatusEvent):void{}
// END #####################################################
}	//class
}	//package
// END #####################################################
